package com.kun.hosp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.commonutil.exception.HospitalException;
import com.kun.commonutil.result.Result;
import com.kun.commonutil.result.ResultCodeEnum;
import com.kun.hosp.service.HospitalSetService;
import com.kun.model.entity.hosp.HospitalSet;
import com.kun.model.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    // http://localhost:8201/admin/hosp/hospitalSet/findAll

    //1 查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }
    //条件查询带分页
    @ApiOperation(value = "获取所有医院设置,条件查询带分页")
    @GetMapping("/findHospitalset/{current}/{limit}")
    public Result<Page<HospitalSet>> findHospitalset(
            @PathVariable Long current,
            @PathVariable Long limit,
            @ApiParam(value = "可传入字段hosName,hosCode") @RequestParam Map<String,Object> param //查询条件
    )
    {
        Page<HospitalSet> page = hospitalSetService.findHospitalset(current,limit,param);
        return Result.ok(page);
    }
    //添加医院信息
    @ApiOperation(value = "添加医院信息")
    @PostMapping("/saveHospitalset")
    public Result<Void> saveHospitalset(
            @RequestBody(required = false) HospitalSet hospitalSet
    )
    {
        hospitalSetService.saveHospitalset(hospitalSet);
        return Result.ok();
    }
    //根据id获取医院信息
    @ApiOperation(value = "根据id获取医院信息")
    @GetMapping("/findHospitalsetById/{id}")
    public Result<HospitalSet> findHospitalsetById(
            @PathVariable(required = false)Long id
    )
    {
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }

    //修改医院信息
    @ApiOperation(value = "修改医院信息")
    @PostMapping("/updateHospitalset")
    public Result<Void> updateHospitalset(
            @RequestBody(required = false) HospitalSet hospitalSet
    )
    {
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //删除医院信息
    @ApiOperation(value = "删除医院信息")
    @GetMapping("/delHospitalsetById")
    public Result<Void> delHospitalsetById(
            @RequestParam(required = false)String ids
    )
    {
        String[] split = ids.split(",");
        hospitalSetService.removeByIds(Arrays.asList(split));
        return Result.ok();
    }
    //医院锁定与解锁
    @ApiOperation(value = "医院锁定与解锁")
    @GetMapping("/lockHospitalset/{id}/{status}")
    public Result<Void> lockHospitalset(
            @PathVariable("id")Long id,
            @PathVariable("status")Integer status
    )
    {
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //发送签名密匙
    @ApiOperation(value = "查询密匙")
    @GetMapping("/sendHospitalsetKey/{id}")
    public Result<String> sendHospitalsetKey(
            @PathVariable("id")Long id
    )
    {
        //TODO 1.验证手机号

        //2.获取密匙
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId.getSignKey());
    }

    //全局异常测试
    @ApiOperation(value = "全局异常测试")
    @GetMapping("/g_exception")
    public Result<Void> g_exception()
    {
        int i = 10 / 0;
        return Result.ok();
    }

    //自定义异常测试
    @ApiOperation(value = "自定义异常测试")
    @GetMapping("/c_exception")
    public Result<Void> c_exception ()
    {
        try {
            int i = 10 / 0;
        }catch (Exception exception)
        {
            throw new HospitalException(ResultCodeEnum.DATA_ERROR);
        }
        return Result.ok();
    }
}
